package priv.mill.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import priv.mill.clien.Client;
import priv.mill.net.MessageContexts;
import priv.mill.net.MessageObj;
import priv.mill.net.MyHttp;
import priv.mill.net.UrlType;

import com.iflytek.view.mk;
import com.iflytek.view.mk2;

/*
 * ������
 */
public class MainView extends JFrame {
	private static Logger logger = Logger.getLogger(MainView.class);
	private JPanel contentPane;
	private JTextField textField;
	mk2 mk2 = new mk2();
	mk mk = new mk();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainView frame = new MainView();
					frame.setVisible(true);
					new Thread(() -> {// �����ͻ���
								new Client().connet("127.0.0.1", 9999);
							}).start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainView() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 674, 540);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		/*
		 * �ı�����ʾ�ظ�����Ϣ
		 */
		JTextArea textArea = new JTextArea(10, 20);

		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setBounds(14, 13, 628, 337);
		textArea.setLineWrap(true);
		contentPane.add(scrollPane);
		/*
		 * �ı�����
		 */
		textField = new JTextField();
		textField.setBounds(14, 387, 415, 60);
		contentPane.add(textField);
		textField.setColumns(10);

		JButton btnNewButton = new JButton("\u53D1\u9001");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String text = textField.getText();// ��ȡ��Ϣ������������Ϣ
				MessageContexts.getInstance().offerMessage(new MessageObj() {

					@Override
					public void handlerMessage(MyHttp info) {
						Object data = info.getData();
						String string = (String) data;
						textArea.append(string + "\n");
						mk.shuo(string);
					}

					@Override
					public MyHttp getMessage() {
						MyHttp myHttp = new MyHttp();
						myHttp.setType(UrlType.NALMORE);
						myHttp.setData(text);
						return myHttp;
					}
				});
			}
		});

		btnNewButton.setBounds(461, 404, 113, 27);
		contentPane.add(btnNewButton);
		/*
		 * 说话按钮
		 */
		JButton speekButton_1 = new JButton("说话");
		speekButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MessageContexts.getInstance().offerMessage(new MessageObj() {

					@Override
					public void handlerMessage(MyHttp info) {
						Object data = info.getData();
						String string = (String) data;
						textArea.append(string + "\n");
						mk.shuo(string);
					}

					@Override
					public MyHttp getMessage() {
						MyHttp myHttp = new MyHttp();
						myHttp.setType(UrlType.NALMORE);
						mk2.Ting();
						String string;
						try {
							Thread.currentThread().sleep(4000);

							if (mk2.finalString == null) {
								string = "我没听清";
							} else {
								string = new String(mk2.finalString);
							}
							myHttp.setData(string);
							mk2.finalString = null;

						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						return myHttp;

					}
				});
			}
		});
		speekButton_1.setBounds(461, 453, 113, 27);
		contentPane.add(speekButton_1);

	}
}
